package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.inf.InfServiceBus;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Description:送达信息
 * File Name:DeliveryInformationAttrHandler
 * Modification History:
 * Date             Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-4-8 9:44   邱萌         1.0       1.0 Version
 **/
public class DeliveryInformationAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private InfServiceBus infServiceBus;
    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        String oppo_init = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "oppo_init");
        if(KiteStringUtils.equals(oppo_init,"T")){
            //商机转定时根据运营方设置运营专员的下拉枚举
            if ("200001046".equals(attr.getFieldName()) && KiteStringUtils.isNotEmpty(attr.getValue())) {
                KiteAttr attachment = inst.getAttr("200004289");
                Map map=new HashMap();
                map.put("carrierId",value);
                List<String> list = infServiceBus.queryOpeList(map);
                List<KiteAttrValue> attrValues = new ArrayList<>();
                list.stream().forEach(name -> {
                    KiteAttrValue attrValue = new KiteAttrValue();
                    attrValue.setAttrValue(name);
                    attrValue.setAttrValueName(name);
                    attrValues.add(attrValue);
                });
                if (Objects.nonNull(attachment)) {
                    attachment.setValue("");
                    attachment.setAttrValues(attrValues);
                }
            }

            //商机转定时根据运营方和业务行业类型设置合作伙伴
/*            if(KiteStringUtils.equalsAny(attr.getFieldName(),"200001046","200001047") && KiteStringUtils.isNotEmpty(attr.getValue())){
                String attr_200001046 = inst.getAttrValueDesc("200001046");
                String attr_200001047 = inst.getAttrValueDesc("200001047");
                if(!KiteStringUtils.isAnyEmpty(attr_200001046,attr_200001047)){
                    String group_user_name = "";
                    List<KiteAttr> groupUserkiteAttrs = querySceneInstService
                        .listCommonAttrsByModuleId(data.getSceneInstId(), LocalKeyConsts.GROUPUSERINFO_MODULEID);
                    if(KiteListUtils.isNotEmpty(groupUserkiteAttrs)){
                        KiteAttr group_user_name_attr = groupUserkiteAttrs.stream().filter(comAttr -> KiteStringUtils.equals(comAttr.getFieldName(), "group_user_name")).findFirst().orElse(null);
                        group_user_name = Optional.ofNullable(group_user_name_attr).map(KiteAttr::getValue).orElse("");
                    }
                    Map map = new HashMap();
                    if (KiteStringUtils.isNotEmpty(group_user_name)) {
                        map.put("ENT_NAME", attr_200001046 + "-" + group_user_name + "-" + attr_200001047);
                        Map<String, Object> detailMap = infServiceBus.queryNewPartnerQuery(map);
                        String RESULT_CODE = (String) detailMap.get("RESULT_CODE");
                        if (RESULT_CODE.equals("0005")) {
                            //ErrorConsts.INF_INVOKE_ERROR.throwOut(LocalCallClientEnum.INF_COMMON_URL.getServiceCode(), "合作伙伴已存在并无法保存，请修改", "");
                        } else {
                            KiteAttr attr2 = inst.getAttr("200001193");
                            attr2.setValue(attr_200001046 + "-" + group_user_name + "-" + attr_200001047);
                            attr2.setValueDesc(attr_200001046 + "-" + group_user_name + "-" + attr_200001047);
                        }
                    }
                }
            }*/
        }
        super.set(attr, inst, value, data);
    }
}
